package gupao.concurrency.lock.lock.forecast;

import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

public class ForcastService {
    @Autowired
    private ExecutorService executorService; //1

    class ForcastTask implements Callable<List<Commodiy>> {
        @Override
        public List<Commodiy> call() throws Exception {
            // w <- (t1,t2)

            List<Traffic> samp = samplingTrafficMirror();
//            List<Traffic> uvSamp = calcByUv(samp);

            Future<List<Traffic>> f = executorService.submit(() -> calcByUv(samp)); //t1
            Future<List<Traffic>> f2 = executorService.submit(() -> calcByEffectivePV(samp));//t2

//            List<Traffic> pvSamp = calcByEffectivePV(samp);
            return conj(f.get(), f2.get());
        }
    }

    @Scheduled
    public void start() throws ExecutionException, InterruptedException {
        Future<List<Commodiy>> f = executorService.submit(new ForcastTask());
        List<Commodiy> list = f.get();
        send(list);
    }

    protected void send(List<Commodiy> list){
        //
    }

    protected List<Commodiy> conj(List<Traffic>... samps){
        return new ArrayList<>();
    }

    protected List<Traffic> calcByUv(List<Traffic> samp){
        return new ArrayList<>();
    }

    protected List<Traffic> calcByEffectivePV(List<Traffic> samp){
        return new ArrayList<>();
    }

    protected List<Traffic> samplingTrafficMirror(){
        List<Traffic> list = new ArrayList<>();
        list.add(new Traffic("1","001"));
        list.add(new Traffic("2","002"));
        list.add(new Traffic("3","003"));
        list.add(new Traffic("4","004"));
        list.add(new Traffic("5","005"));
        list.add(new Traffic("6","006"));
        list.add(new Traffic("7","007"));
        list.add(new Traffic("8","008"));
        return list;
    }

    @AllArgsConstructor
    static class Traffic{
        String commId;
        String uid;
    }

    static class Commodiy{
    }
}
